package com.common.util;

import org.openqa.selenium.Cookie;

import java.util.Set;

/**
 * 对浏览器的cookie进行获取和增加的操作。
 * Created by teabot on 2017/8/3.
 */
public class UseCookieUtil extends WebDriverUtil{
    //用户cookie
    private static Cookie userCookie;
    //用户cookie名称
    private final static String COOKIENAME="JSESSIONID";

    //根据cookie名称获取cookie相关值
    public  Cookie myGetCookie(String ckName) {
        //获取当前所有cookie
        Set<Cookie> cookies = driver.manage().getCookies();
        for (Cookie ck : cookies) {
            //获取cookie名称对应的cookie相关
            if (ck.getName().equals(ckName)) {
                userCookie = ck;
//                System.out.println(userCookie);
            }
        }
        return userCookie;
    }

    //动态增加cookie值
    public void setCookie(){
        driver.manage().addCookie(myGetCookie(COOKIENAME));
    }

    //删除所有cookie值
    public void delCookie(){
        driver.manage().deleteAllCookies();
    }
}
